/*
 * @Author: SKY-20210422HOK\Administrator 1174123267@
 * @Date: 2022-07-04 09:31:20
 * @LastEditors: SKY-20210422HOK\Administrator 1174123267@
 * @LastEditTime: 2022-07-25 11:05:34
 * @FilePath: \jrzb-user\src\utils\request.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import axios from "axios";
import { Loading, Message } from "element-ui";
import router from '@/router'
let loading;
// eslint-disable-next-line no-unused-vars
let loadingInstance = null;
const baseURL = "http://111.229.46.23:8182";
// axios.defaults.baseURL= '/api'
// const baseURL = "http://47.76.228.175:1443";
axios.defaults.baseURL=baseURL;
axios.defaults.headers.post['Content-Type'] = 'application/json';
export function endLoading() {
  if (loading) {
    loading.close();
  }
}
export function MessageSuccess(msg) {
  Message({
    message: msg || "请求成功",
    type: "Success",
    duration: 5 * 1000,
  });
}
export function MessageError(msg) {
  Message({
    message: msg || "请求失败",
    type: "error",
    duration: 5 * 1000,
  });
}

const service = axios.create({
  baseURL: process.env.VUE_APP_BASE_API,
  timeout: 10000,
});
// 请求拦截
service.interceptors.request.use(
  (config) => {
    //loadingInstance 判断是否存在多个遮罩层 不存在就生成 存在就取消
    if(!loadingInstance){
      loadingInstance = Loading.service({
        lock: true, // 是否锁屏
        text: "正在加载...", // 加载动画的文字
        spinner: "el-icon-loading", // 引入的loading图标
        background: "rgba(0, 0, 0, 0.3)", // 背景颜色
        target: ".sub-main", // 需要遮罩的区域
        body: true,
        customClass: "mask", // 遮罩层新增类名
        fullscreen: false, // 全屏开关
      });
    }

    // config.headers["token"] = sessionStorage.getItem('token')?sessionStorage.getItem('token'):'';
    return config;
  }, (error) => {
    MessageError; // for debug
    return Promise.reject(error);
  }
);
// 请求响应的拦截器
service.interceptors.response.use(
  (response) => {
    loadingInstance.close();
    const {code}=response.data
    if (code==401){
      MessageError("登录已过期");
       router.push('/login')
    }
    return response;
  }, (error) => {
    // 关闭遮罩层 加载中
    loadingInstance.close();
    MessageError("服务器异常");
    return Promise.reject(error);
  }
);
export function get(url, params, headers) {
  return new Promise(function (resolve, reject) {
    service.get(url, {
      params,  // 请求参数
      headers // 请求头
    })
      .then(res => {
        resolve(res.data) // res.data 当做成功回调的参数返回出去
      })
      .catch(err => {
        reject(err)
      })
  })
}
export function post(url, body, headers) {
  // return new Promise(function (resolve, reject) {
    return service.post(url, body, {
      ...headers // 请求头
    })
      // .then(res => {
      //   console.log('postres',res)
      //   // console.log(res)
      //   resolve(res.data)
      // })
      //拦截处理二次出错 ！！
      // .catch(err => {
      //   console.log('posterr',err)
      //   reject(err)
      // })
  // })
}

export function patch(url, params, headers) {
  return new Promise(function (resolve, reject) {
    axios({
      url,
      method: "patch",
      data: params,
    })
      .then(res => {
        resolve(res.data)
      })
      .catch(err => {
        reject(err)
      })
  })
}

export function Delete(url, params, headers) {
  return new Promise(function (resolve, reject) {
    axios({
      url,
      method: "delete",
      data: params,
    })
      .then(res => {
        resolve(res.data)
      })
      .catch(err => {
        reject(err)
      })
  })
}
export {
  axios,
  // baseURL
};
